<template>
  <div class="article-card" @click="goToDetail(props.article._id as string)">
    <!-- 左侧图片区域 -->
    <div class="card-image">
      <el-image :src="$props.article?.cover" fit="cover">
        <template #placeholder>
          <Loading />
        </template>
      </el-image>
    </div>

    <!-- 右侧内容区域 -->
    <div class="card-content">
      <!-- 标题 -->
      <h2 class="title">{{ props.article?.title }}</h2>

      <!-- 描述 -->
      <p class="description">{{ props.article?.desc }}</p>

      <!-- 标签区域 -->
      <div class="tags">
        <el-tag size="small" type="info" class="tag-item">
          <SvgIcon name="article-classs" style="margin-right: 5px" />
          <span>{{ props.article?.aboutInfo.category.name }}</span>
        </el-tag>

        <div class="item" v-for="i in $props.article?.tags as Tag[]" :key="i._id">
            <img :src="i.icon" alt="" />
        </div>
      </div>

      <!-- 底部信息 -->
      <div class="footer">
        <span class="author">{{ props.article.aboutInfo?.user?.name }}</span>
        <span class="date">{{ dayjs(props.article?.createTime).format("YYYY-MM-DD HH:mm:ss") }}</span>
        <div class="metrics">
          <span class="metric-item">
            <el-icon><View /></el-icon>
            {{ props.article?.viewNum }}
          </span>
          <span class="metric-item">
            <el-icon><ChatRound /></el-icon>
            {{ props.article?.commentNum }}
          </span>
          <span class="metric-item" @click.stop="addLike">
            <el-icon><Star /></el-icon>
            {{ props.article?.likeNum }}
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineProps } from "vue"
import { ArticleItem } from "@/types/admin/article"
import type { Tag } from "@/types/admin/tags"
import dayjs from "dayjs"
import { ref } from "vue"
import { useRouter } from "vue-router"
import { useSettingsStore } from "@/store/modules/settings"
import Loading from "@/components/Loading/index.vue"
const settingStore = useSettingsStore()

// 接受父组件按数据
const props = defineProps<{
  index: number
  article: ArticleItem
}>()

// 全局路由对象
const router = useRouter()

// 防抖
const isLike = ref<boolean>(true)

// 提交事件
const emit = defineEmits(["addLike"])

// 点赞
const addLike = () => {
  if (isLike.value) {
    isLike.value = false
    // 通知父组件更新点赞量
    emit("addLike")
    setTimeout(() => {
      isLike.value = true
    }, 1000)
  }
}

// 跳转详情页面
const goToDetail = (id: string) => {
  router.push({
    path: "/home-detail",
    query: {
      id
    }
  })
}
</script>

<style scoped lang="scss">
.article-card {
  display: flex;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  cursor: pointer;
  margin-bottom: 20px;

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 4px 20px 0 rgba(0, 0, 0, 0.15);
  }

  .card-image {
    width: 200px;
    height: 120px;
    margin-right: 20px;
    border-radius: 6px;
    overflow: hidden;
    flex-shrink: 0;
    .el-image {
      width: 100%;
      height: 100%;
      object-fit: cover;
      transition: transform 0.3s ease;

      &:hover {
        transform: scale(1.05);
      }
    }
  }

  .card-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    
    .title {
      max-width: 540px;
      height: 20px;
      font-size: 20px;
      font-weight: 600;
      color: #303133;
      margin: 0 0 12px 0;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .description {
      font-size: 14px;
      color: #606266;
      margin: 0 0 16px 0;
      height: 15px;
      overflow: hidden;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }

    .tags {
      margin-bottom: 10px;
      display: flex;
      .tag-item {
        height: 20px;
        margin-right: 8px;
        margin-bottom: 8px;
      }
      .item {
        margin: 0 5px;
        img {
          width: 16px;
          height: 16px;
        }
      }
      :deep(.el-tag__content){
        display: flex;
        align-items: center;
        line-height: 20px;
        font-size: 12px;
      }
    }
    .footer {
      margin-top: auto;
      display: flex;
      align-items: center;
      font-size: 14px;
      color: #909399;

      .author {
        font-weight: 500;
      }

      .date {
        margin: 0 16px;
      }

      .metrics {
        display: flex;
        align-items: center;

        .metric-item {
          display: flex;
          align-items: center;
          margin-left: 16px;

          .el-icon {
            margin-right: 4px;
            font-size: 16px;
          }

          &:hover {
            color: #409eff;
          }
        }
      }
    }
  }
}

// 响应式设计
@media screen and (max-width: 768px) {
  .article-card {
    flex-direction: column;

    .card-image {
      width: 100%;
      height: 200px;
      margin-right: 0;
      margin-bottom: 16px;
    }

    .footer {
      flex-direction: column;
      align-items: flex-start;
      gap: 8px;

      .date {
        margin: 8px 0;
      }

      .metrics {
        width: 100%;
        justify-content: space-around;

        .metric-item {
          margin-left: 0;
        }
      }
    }
  }
}
</style>
